import utils from '@/src/utils.js'
import consts from '@/src/consts.js'

/**
 * 解密微信返回的信息
 * @param {code} code wx.login返回的授权码
 * @param {string} iv wx.getUserInfo在withCredential:true的条件下返回的iv(加密初始化向量)
 * @param {string} encryptedData wx.getUserInfo返回的加密敏感数据
 * */
async function getWeiXinInfo(code, iv, encryptedData) {
  const data = await utils.request.default.post('/api/user/account/v1/wx/getwxinfo', {
    code,
    iv,
    encryptedData
  })
  return data.data
}
 
/**
 * 登录接口
 * @param {object} wxInfo 微信加密数据encryptedData解密出来的数据
 * @param {string} accessToken 微信的accessToken
 * */
async function getLogin(wxInfo, accessToken) {
  const data = await utils.request.default.post('/api/user/account/v1/login/login', {
    account: `${wxInfo.openId}_sadais_wx`,
    loginway: 'byweixin',
    unionid: wxInfo.unionId || wxInfo.openId,
    openid: wxInfo.openId,
    accesstoken: accessToken,
    from: consts.WX_APP_FROM,
    name: wxInfo.nickName,
    sex: wxInfo.gender === 1 ? '男' : '女',
    headpic: wxInfo.avatarUrl
  })
  return data.data
}

module.exports = {
	login: {
		getWeiXinInfo,
		getLogin
	}
}